package com.czxy.service;

import com.czxy.mapper.SkuRepository;
import com.czxy.vo.ReturnSku;
import com.czxy.vo.SearchResult;
import com.czxy.vo.SearchSku;
import com.czxy.vo.SkuSearchRequest;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

/**
 * @author 张青卯
 */
@Service
public class SkuSearchService {

    private final SkuRepository skuRepository;

    @Autowired
    public SkuSearchService(SkuRepository skuRepository) {
        this.skuRepository = skuRepository;
    }

    public Object search(SkuSearchRequest req) {
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.termQuery("cat3_id",req.getCatid()));
        if (req.getBrand_id()!=null && !"".equals(req.getBrand_id())){
            boolQueryBuilder.must(QueryBuilders.termQuery("brand_id",req.getBrand_id()));
        }
        HashMap<String,String> map = null;
        if (StringUtils.isNotBlank(req.getSpec_list())){
            map = new HashMap<>(3);
            String[] strings = req.getSpec_list().split(",");
            for (String s : strings) {
                String[] split = s.split("=");
                if (split.length==1){
                    map.remove(split[0]);
                }else {
                    map.put(split[0],split[1]);
                }
            }
        }
        if (map != null){
            Set<String> keys = map.keySet();
            for (String key : keys) {
                String value = map.get(key);
                key = "specs." + key + ".keyword";
                boolQueryBuilder.must(QueryBuilders.termQuery(key,value));
            }
        }
        if (req.getMin_price() != null){
            boolQueryBuilder.must(QueryBuilders.rangeQuery("price").gte(req.getMin_price()));
        }
        if (req.getMax_price() != null){
            boolQueryBuilder.must(QueryBuilders.rangeQuery("price").lte(req.getMax_price()));
        }
        String xl = "xl";String pl = "pl";String sj = "sj";String jg = "jg";String asc = "asc";String desc = "desc";
        if (xl.equals(req.getSort_by())){
            queryBuilder.withSort(SortBuilders.fieldSort("seller_count").order(SortOrder.DESC));
        }else if (pl.equals(req.getSort_by())){
            queryBuilder.withSort(SortBuilders.fieldSort("comment_count").order(SortOrder.DESC));
        }else if (sj.equals(req.getSort_by())){
            queryBuilder.withSort(SortBuilders.fieldSort("on_sale_time").order(SortOrder.DESC));
        }else if (jg.equals(req.getSort_by().toLowerCase())&&asc.equals(req.getSort_way())){
            queryBuilder.withSort(SortBuilders.fieldSort("price").order(SortOrder.ASC));
        }else if (jg.equals(req.getSort_by().toLowerCase())&&desc.equals(req.getSort_way())){
            queryBuilder.withSort(SortBuilders.fieldSort("price").order(SortOrder.DESC));
        }
        queryBuilder.withQuery(boolQueryBuilder);
        int pageNumn = req.getPage();
        int size = req.getLimit();
        queryBuilder.withPageable(PageRequest.of(pageNumn-1,size));
        Page<SearchSku> page = this.skuRepository.search(queryBuilder.build());
        long count = page.getTotalElements();
        Integer catId = req.getCatid();
        ArrayList<ReturnSku> list = new ArrayList<>();
        for (SearchSku s : page) {
            ReturnSku sku = new ReturnSku();
            sku.setId(s.getId());
            sku.setGoods_name(s.getSku_name());
            sku.setPrice(s.getPrice());
            sku.setMidlogo(s.getLogo());
            sku.setComment_count(s.getComment_count());
            list.add(sku);
        }
        return new SearchResult(0, "成功", list, (int) count, catId);
    }
}
